#!/usr/bin/python3
# -*- coding:utf-8 -*-
# __author__ == taoyulong2018@gmail.com
# __time__ == 2023/8/29 17:01
# ===========================================
#       题目名称： 49. 字母异位词分组
#       题目地址： https://leetcode.cn/problems/rotate-image/description/
#       题目描述： https://note.youdao.com/s/RRXNSXjR
# ===========================================
from typing import List


class Solution:
    """
        实现思路：
            字典、排序
    """

    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
        res_dict = dict()  # k 存放排序后的单词 v c存放未排序的单词
        while strs:
            element = strs.pop()
            sorted_element = tuple(sorted(element))
            if sorted_element in res_dict.keys():
                res_dict[sorted_element].append(element)
            else:
                res_dict[sorted_element] = [element, ]
        return list(res_dict.values())


if __name__ == '__main__':
    s = Solution()
    # [["bat"],["nat","tan"],["ate","eat","tea"]]
    print(s.groupAnagrams(strs=["eat", "tea", "tan", "ate", "nat", "bat"]))
    # [[""]]
    print(s.groupAnagrams(strs=[""]))
    # [["a"]]
    print(s.groupAnagrams(strs=["a"]))
